package com.wang.wx.controller;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.wang.wx.common.Result;
import com.wang.wx.entity.EmailUser;
import com.wang.wx.service.DataSplitterService;
import com.wang.wx.service.EmailUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * (EmailUser)表控制层
 *
 * @author makejava
 * @since 2024-08-02 14:54:08
 */
@RestController
@RequestMapping("/admin/system/emailUser")
@Api(tags = "1、邮箱数据管理")
public class EmailUserController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private EmailUserService emailUserService;

    @Resource
    private DataSplitterService emailDataSplitterServiceImpl;

    /**
     * 数据入库
     */
    @PostMapping("/upload")
    @ApiOperation(value = "输入文件路径，解析入库")
    public R dataReceipt(String filePath) throws IOException {
        emailDataSplitterServiceImpl.handleFile(filePath);
        return success("ok");
    }


    @ApiOperation(value = "2、创建索引")
    @GetMapping("/createIndex")
    public Result createIndex() {
        try (HintManager hintManager = HintManager.getInstance()) {
            // 指定数据源名称
            hintManager.setDataSourceName("ds_email");

            // 执行存储过程
            emailUserService.createIndexes();

            return Result.ok("创建邮箱索引成功");
        } catch (Exception e) {
            throw new RuntimeException("已存在相同的邮箱索引，不要重复创建", e);
        }
    }

    @ApiOperation(value = "3、删除索引")
    @GetMapping("/dropIndex")
    public Result dropIndex() {
        try (HintManager hintManager = HintManager.getInstance()) {
            // 指定数据源名称
            hintManager.setDataSourceName("ds_email");

            // 执行存储过程
            emailUserService.dropIndexes();

            return Result.ok("删除邮箱索引成功");
        } catch (Exception e) {
            throw new RuntimeException("已存在相同的邮箱索引，不要重复创建", e);
        }
    }
}

